package com.sscw.task.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.guaike.core.mybatis.plugins.BaseDAO;
import com.sscw.task.entity.GRecordGamePO;
import com.sscw.task.enums.RoomTypeEnum;
import com.sscw.task.mapper.GRecordGameExtMapper;
import com.sscw.task.mapper.GRecordGameMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Repository
@AllArgsConstructor
public class GRecordGameDAO extends BaseDAO<GRecordGameMapper, GRecordGamePO> {

    private final GRecordGameExtMapper gRecordGameExtMapper;

    public List<GRecordGamePO> findRankRecord(Integer roomType, LocalDate yesToday, Integer size) {
        return gRecordGameExtMapper.findRankRecord(roomType, yesToday, size);
    }

    public List<GRecordGamePO> findNeedSettle() {
        LambdaQueryWrapper<GRecordGamePO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.isNull(GRecordGamePO::getEndTime);
        queryWrapper.lt(GRecordGamePO::getFinallyCoinTime, LocalDateTime.now().minusSeconds(120));
        queryWrapper.and(wrapper -> wrapper.eq(GRecordGamePO::getRoomType, RoomTypeEnum.arcade_machine.getCode()).or().eq(GRecordGamePO::getRoomType, RoomTypeEnum.recreation.getCode()));
        return getBaseMapper().selectList(queryWrapper);
    }

    public long checkPlaying(Long deviceId){
        LambdaQueryWrapper<GRecordGamePO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GRecordGamePO::getDeviceId,deviceId);
        queryWrapper.gt(GRecordGamePO::getEndTime, LocalDateTime.now());
        return getBaseMapper().selectCount(queryWrapper);
    }
}
